package com.kangfu.api.service.impl;

import com.kangfu.api.service.IAddressService;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.db.domain.entity.Address;
import com.kangfu.db.mapper.AddressMapper;
import com.kangfu.db.model.request.AddressRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {

    @Autowired
    public AddressMapper addressMapper;

    @Override
    public List<Address> getAddressList(int userId, int page){
        if (page <= 1) {
            page = 1;
        }
        int pageSize = 5;
        int offset = (page -1) * pageSize;
        return addressMapper.selectAddressList(userId, offset, pageSize);
    }

    @Override
    public Address getAddress(int userId, int id) {
        //先查查是不是自己的地址
        Address findAddress = addressMapper.selectAddressById(id);
        if (findAddress == null || userId != findAddress.getUserId() || 2 == findAddress.getDelFlag()) {
            throw new BusinessException("不存在的地址");
        }
        return findAddress;
    }

    @Override
    public Integer insertAddress(int userId, AddressRequest addressRequest) {
        Address address = new Address();
        address.setUserId(userId);
        address.setName(addressRequest.getName());
        address.setPhone(addressRequest.getPhone());
        address.setArea(addressRequest.getArea());
        address.setAreaCode(addressRequest.getAreaCode());
        address.setAddress(addressRequest.getAddress());
        if (1 == addressRequest.getDefaultFlag()) {
            //如果这个是默认地址，先检查有没有默认地址，如果已经有了就把它变成非默认
            Address defaultAddress = addressMapper.selectDefaultAddress(userId);
            if (defaultAddress != null) {
                addressMapper.setUnDefaultAddress(defaultAddress.getId());
            }
        }
        address.setDefaultFlag(addressRequest.getDefaultFlag());
        address.setCreateTime(LocalDateTime.now());
        return addressMapper.insertAddress(address);
    }

    @Override
    public Integer updateAddress(int userId,int id, AddressRequest addressRequest) {
        //先查查是不是自己的地址
        Address findAddress = addressMapper.selectAddressById(id);
        if (findAddress == null || userId != findAddress.getUserId() || 2 == findAddress.getDelFlag()) {
            throw new BusinessException("不存在的地址");
        }
        Address address = new Address();
        address.setId(findAddress.getId());
        address.setName(addressRequest.getName());
        address.setPhone(addressRequest.getPhone());
        address.setArea(addressRequest.getArea());
        address.setAreaCode(addressRequest.getAreaCode());
        address.setAddress(addressRequest.getAddress());
        if (1 == addressRequest.getDefaultFlag()) {
            //如果这个是默认地址，先检查有没有默认地址，如果已经有了就把它变成非默认
            Address defaultAddress = addressMapper.selectDefaultAddress(userId);
            if (defaultAddress != null) {
                addressMapper.setUnDefaultAddress(defaultAddress.getId());
            }
        }
        address.setDefaultFlag(addressRequest.getDefaultFlag());
        address.setUpdateTime(LocalDateTime.now());
        return addressMapper.updateAddress(address);
    }

    @Override
    public Integer delAddress(int userId,int id) {
        //先查查是不是自己的地址
        Address findAddress = addressMapper.selectAddressById(id);
        if (findAddress == null || userId != findAddress.getUserId() || 2 == findAddress.getDelFlag()) {
            throw new BusinessException("不存在的地址");
        }
        return addressMapper.delAddress(id);
    }
}
